package com.lvtu.monitor.processer;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lvmama.com.lvtu.common.httpsqs4j.msg.HttpsqsMessage;
import com.lvmama.com.lvtu.common.httpsqs4j.msg.HttpsqsMessageProcesser;
import com.lvtu.monitor.dao.exmatch.po.ExDetail;
import com.lvtu.monitor.notification.mail.ObserverRunable;
import com.lvtu.monitor.utils.Constants;
import com.lvtu.monitor.utils.ElasticUtils;
import com.lvtu.monitor.utils.ThreadPoolUtils;

/** 
 * @Title: ErrorMessageProcesser.java 
 * @Package com.lvtu.monitor.customer 
 * @Description: TODO 
 * @author dengcheng 
 * @date 2015-5-12 上午10:20:41 
 * @version V1.0.0 
 */
public class ErrorMessageProcesser implements HttpsqsMessageProcesser{
	protected ThreadPoolExecutor threadPoolExecutor;

	@Autowired
	ObserverRunable observerRunnable;
	
	public static Map<String, Object> getUrlParams(String param) {  
	    Map<String, Object> map = new HashMap<String, Object>(0);  
	    if (StringUtils.isBlank(param)) {  
	        return map;  
	    }  
	    String[] params = param.split("&");  
	    for (int i = 0; i < params.length; i++) {  
	        String[] p = params[i].split("=");  
	        if (p.length == 2) {  
	            map.put(p[0], p[1]);  
	        }  
	    }  
	    return map;  
	} 
	
	@Override
	public void process(HttpsqsMessage message) {
		if(message.isClientServiceExceptionTraceMessage()){
			JSONObject obj = JSON.parseObject(message.getAddition());
			String queryString = (String)obj.get("queryString");
			Map<String,Object> paramMap = getUrlParams(queryString);
			String udid = (String)paramMap.get("udid");
			String method = (String)paramMap.get("method");
			String firstChannel = "";
			if(paramMap.get("firstChannel")!=null){
				firstChannel =(String) paramMap.get("firstChannel");
			}
			String secondChannel = "";
			if(paramMap.get("secondChannel")!=null){
				secondChannel =(String) paramMap.get("secondChannel");
			}
			String version = (String)paramMap.get("version");
			String lvsessionid = (String)paramMap.get("lvsessionid");
			String ip = obj.getString("ip");
			
			Long createdTime = obj.getLong("createdTime");
			String userAgent = obj.getString("userAgent");
			String exception = obj.getString("exception");
			
			ExDetail exDetail = new ExDetail();
			exDetail.setUdid(udid);
			exDetail.setMethod(method);
			exDetail.setUserAgent(userAgent);
			exDetail.setChannel(firstChannel+"_"+secondChannel);
			exDetail.setIp(ip);
			exDetail.setException(exception);
			exDetail.setCreatedTime(createdTime);
			exDetail.setQueryString(queryString);
			exDetail.setLvsessionid(lvsessionid);
			exDetail.setVersion(version);
			this.startObserverThread(exDetail);
			try {
				/**
				 * 将错误信息写入索引
				 */
				ElasticUtils.getInstance().putIndex(Constants.ELASTIC_INDEX_EX_TRACE, Constants.ELASTIC_TYPE_EX_TRACE, JSON.toJSONString(exDetail));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
	public void startObserverThread(ExDetail ex){
		observerRunnable.setEx(ex);
		ThreadPoolUtils.getInstance().getThreadPoolExecutor().execute(observerRunnable);
	}

}
